package com.am.mosl.cityandroid.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {  
    public static final int COLUMN_ID = 1;  
    public static final int COLUMN_INDEX = 0;  
    public static final int COLUMN_NAME = 2;  
    public static final int COLUMN_URL = 3;  
    public static final String ID_STRING = "id";  
    private static DatabaseHelper mDatabaseHelper = null;  
    public static final String NAME = "mydatabase.db";  
    public static final String TABLE_NAME = "web_broadcast";  
    private static final int VERSION = 1;  
  
    public static DatabaseHelper getDatabaseHelper(Context context) {  
        if (mDatabaseHelper == null) {  
            mDatabaseHelper = new DatabaseHelper(context);  
        }  
        return mDatabaseHelper;  
    }  
  
    private String TABLE_BROADCAST = "name";  
    private String TABLE_ID = ID_STRING;  
    private String TABLE_INDEX = "index_num";  
    private String TABLE_URL = "url";  
  
    // private String TABLE_NUM="_num";  
    // private String TABLE_DATA="_data";  
    private DatabaseHelper(Context context) {  
        super(context, NAME, null, VERSION);  
        // TODO Auto-generated constructor stub  
    }  
  
    /** 
     * /** 删除记录 
     *  
     *  
     */  
    public void delete(String index) {  
        SQLiteDatabase db = getWritableDatabase();  
        db.delete(TABLE_NAME, "index_num=?", new String[] { index });  
    }  
  
    /** 
     * 添加数据 
     */  
    public long insert(String id, String name, String url) {  
        SQLiteDatabase db = getWritableDatabase();// 获取可写SQLiteDatabase对象  
        // ContentValues类似map，存入的是键值对  
        ContentValues contentValues = new ContentValues();  
        contentValues.put(TABLE_ID, id);  
        contentValues.put(TABLE_BROADCAST, name);  
        contentValues.put(TABLE_URL, url);  
        return db.insert(TABLE_NAME, null, contentValues);  
    }  
  
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        // TODO Auto-generated method stub  
        String sql = "CREATE TABLE " + TABLE_NAME + "(" + TABLE_INDEX  
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + TABLE_ID + " text,"  
                + TABLE_BROADCAST + " text," + TABLE_URL + " text)";  
        // String sql = "CREATE TABLE "  
        // + TABLE_NAME  
        // + "(_idnum INTEGER PRIMARY KEY AUTOINCREMENT,_num text,_data text)";  
        db.execSQL(sql);// 需要异常捕获  
    }  
  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
        // TODO Auto-generated method stub  
        String sql = "drop table " + TABLE_NAME;  
        db.execSQL(sql);  
        onCreate(db);  
  
    }  
  
    public Cursor query(String selection, String[] selectionArgs) {  
        SQLiteDatabase db = getReadableDatabase();  
        Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null,  
                "index_num desc");  
        return cursor;  
    }  
  
    /** 
     * 查询所有数据 
     *  
     * @return Cursor 
     */  
    public Cursor select() {  
        SQLiteDatabase db = getReadableDatabase();  
        Cursor cursor = db.query(TABLE_NAME, new String[] { "index_num",  
                TABLE_ID, TABLE_BROADCAST, TABLE_URL }, null, null, null, null,  
                "index_num desc"); // desc 降序排列,asc 升序排列  
        return cursor;  
    }  
  
    /** 
     * 更新记录的，跟插入的很像 
     */  
    public void update(String index, String id, String name, String url) {  
        SQLiteDatabase db = getWritableDatabase();  
        ContentValues contentValues = new ContentValues();  
        contentValues.put(TABLE_ID, id);  
        contentValues.put(TABLE_BROADCAST, name);  
        contentValues.put(TABLE_URL, url);  
        db.update(TABLE_NAME, contentValues, "index_num=?",  
                new String[] { index });  
    }  
  
}  